package logic;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;

import logic.strategy.ConquerDoubleStrategy;
import logic.strategy.Strategy;

public class MissionDoubleConquer extends MissionConquerContinent implements Serializable {
	
	private static final long serialVersionUID = 1L;
	private Continent secondContinent;
	private String mission;
	
	public MissionDoubleConquer(Continent first, Continent second){
		super(first);
		this.secondContinent = second;
		this.mission = "need to conquer " + first + " and " + second + ".\nThe Countries to counrqe are:\n";
		for(int i=0; i<continentToConquer.getTerritoriesContained().size(); i++){
			this.mission += continentToConquer.getTerritoriesContained().get(i).getName() + ", ";
		}
		for(int i=0; i<secondContinent.getTerritoriesContained().size()-1; i++){
			this.mission += secondContinent.getTerritoriesContained().get(i).getName() + ", ";
		}
		this.mission += secondContinent.getTerritoriesContained().get(secondContinent.getTerritoriesContained().size()-1).getName();
	}
	
	public MissionDoubleConquer(Continent first, Continent second, int soldiers){
		super(first,soldiers);
		this.secondContinent = second;
		this.mission = "need to conquer " + first + " and " + second + " by placing at least " + soldiers + " soldiers.\nThe Countries to counrqe are:\n";
		for(int i=0; i<continentToConquer.getTerritoriesContained().size(); i++){
			this.mission += continentToConquer.getTerritoriesContained().get(i).getName() + ", ";
		}
		for(int i=0; i<secondContinent.getTerritoriesContained().size()-1; i++){
			this.mission += secondContinent.getTerritoriesContained().get(i).getName() + ", ";
		}
		this.mission += secondContinent.getTerritoriesContained().get(secondContinent.getTerritoriesContained().size()-1).getName();
	}
	
	public boolean equals(Mission mission) {
		if(!(mission instanceof MissionDoubleConquer)) return false;
		MissionDoubleConquer temp = (MissionDoubleConquer)mission;
		if(!temp.continentToConquer.equals(this.continentToConquer)) return false;
		if(!this.secondContinent.equals(temp.secondContinent)) return false;
		return true;
	}

	public String getMission() {
		return this.mission;
	}

	public Strategy getStrategy() {
		return new ConquerDoubleStrategy(this.continentToConquer, this.secondContinent);
	}

	public boolean isLegal(Player player) {
		return true;
	}

	public boolean isMissionAccomplished(Player player) {
		return continentToConquer.isOwnedBy(player, numberOfSoldiers) && secondContinent.isOwnedBy(player, numberOfSoldiers);
	}

	public boolean saveMission(BufferedWriter bwriter) {
		try {
			bwriter.write("conquer:" + continentToConquer.getName()+ ":" + secondContinent + ":" + numberOfSoldiers);
		} catch (IOException e) {
			System.err.println("An error occured while saving, please try again later.");
			return false;
		}
		return true;
	}

}
